from typing import Optional

from aiogram_dialog import DialogManager
from sqlalchemy.ext.asyncio import AsyncSession

from app.bot.services.locale import Locale
from app.database.services.providers import query_providers


async def providers_getter(
    db_session: AsyncSession, dialog_manager: DialogManager, **kwargs
):

    proxy_type: Optional[str] = dialog_manager.dialog_data.get("proxy_type", "mobile")
    proxy_privacy: str = dialog_manager.dialog_data.get("proxy_privacy", "private")
    country_id: Optional[int] = dialog_manager.dialog_data.get("country_id")

    locale: Locale = dialog_manager.middleware_data.get("locale")
    if locale.translators[0].locale == "en":
        user_language = "en"
    else:
        user_language = "ru"

    providers = await query_providers(db_session, proxy_type, proxy_privacy, country_id)

    return {
        "providers": providers,
        "count": len(providers),
        "user_language": user_language,
    }
